# -*- coding: utf-8 -*-
"""
Created on Wed Aug 17 11:02:50 2022

@author: wulong
"""
import numpy as np
from casadi import *
from Basic_paras import *

# In[1] Slow EMPC model
# States
def ode_ies_s(xs, xf, us, uf, uz, ud):
    dxdt = SX.zeros(Nx_s)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = xf[0]
    Gh2 = xf[1]
    pO2 = xf[2]
    pH2O = xf[3]
    pH2 = xf[4]
    Pmtf = xf[5]
    tabf = xf[6]
    tabw = xf[7]
    tabt = xf[8]
    tc = xf[9]
    tcs = xf[10]
    tcwm = xf[11]
    te = xf[12]
    tes = xf[13]
    tewm = xf[14]
    vcap = xf[15]
    Iba = xf[16]
    tre = xf[17]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = uf[0]
    Gfm = uf[1]
    Nec = uf[2]
    Pbar = uf[3]
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # real ST input with binary
    Gst = zst*Gstu + (zst - 1)*Gstu
    mstc = C_sot*Mst
    
    # ST in the return side
    msth = Mst - mstc
    tsth = C_sth/(msth + eps)
    thp = zst*tre + (1 - zst)*tsth
    
    # PIP in the rerurn side
    Gsl = Gab + Gec + Gst
    trec = (Gsl*tre - Gst*thp)/(Gab + Gec + eps)
    
    # MA
    tab = zma*(tab0 + tabf + tabw + tabt)
    
    # EC
    tec = zec*(2*tewm - trec)
    
    # PIP in the supply side 1
    tslc = (Gab*tab + Gec*tec)/(Gab + Gec + eps)
    
    # ST in the supply side
    tstc = C_stc/(mstc + eps)
    tcp = zst*(tstc) + (1 - zst)*tslc
    
    # PIP in the supply side 2
    tsl = (Gab*tab + Gec*tec + Gst*tcp)/(Gsl + eps)
    
    # BR
    Qsl = Gsl*Cw*(tre - tsl)
    
    # BA
    iba = Iba/npb
    
    dxdt[0] = -iba/(3600*Ceb)
    dxdt[1] = -Gst/Mst
    dxdt[2] = -Gst*tcp
    dxdt[3] = Gst*thp
    dxdt[4] = (Ubr*(ta - tbr) - Qsl + Qother)/Cbr
    
    return dxdt

# Constranints on fast states
def const_ies_s(xs, xf, us, uf, uz, ud):
    x_f_const = MX.zeros(Nx_f)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = xf[0]
    Gh2 = xf[1]
    pO2 = xf[2]
    pH2O = xf[3]
    pH2 = xf[4]
    Pmtf = xf[5]
    tabf = xf[6]
    tabw = xf[7]
    tabt = xf[8]
    tc = xf[9]
    tcs = xf[10]
    tcwm = xf[11]
    te = xf[12]
    tes = xf[13]
    tewm = xf[14]
    vcap = xf[15]
    Iba = xf[16]
    tre = xf[17]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = uf[0]
    Gfm = uf[1]
    Nec = uf[2]
    Pbar = uf[3]
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # real ST input with binary
    Gst = zst*Gstu + (zst - 1)*Gstu # ST
    mstc = C_sot*Mst
    
    # FC
    Ifc = fu_d/(2*Kr)*Gh2
    Gff_mol = Gff*Mch4
    Go2 = 1/r_HO*Gh2
    Gh2r = 2*Kr*If
    
    # ST in the return side
    msth = Mst - mstc
    tsth = C_sth/(msth + eps)
    thp = zst*tre + (1 - zst)*tsth
    
    # PIP in the rerurn side
    Gsl = Gab + Gec + Gst
    trec = (Gsl*tre - Gst*thp)/(Gab + Gec + eps)
    
    # MA
    tab = zma*(tab0 + tabf + tabw + tabt)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    hcro = -137.26+1.23463*(273.15+tc) - hspc
    hero = 338.02893+0.24532*(273.15+te) + hsph
    tcwo = 2*tcwm - tcwi
    tec = zec*(2*tewm - trec)
    pr = pc/pe # Compressor
    etavl = 0.98-0.085*(pr**(1/kr)-1)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(pr**((kr-1)/kr)-1)
    hcri = hero + wi
    heri = hcro # Expansion valve
    
    # PIP in the supply side 1
    tslc = (Gab*tab + Gec*tec)/(Gab + Gec + eps)
    
    # ST in the supply side
    tstc = C_stc/(mstc + eps)
    tcp = zst*(tstc) + (1 - zst)*tslc
    
    # PIP in the supply side 2
    tsl = (Gab*tab + Gec*tec + Gst*tcp)/(Gsl + eps)
    
    # BR
    Q_the = Qsl0*(tbr - tsl)/delta_twa0*(Gsl/Gsl0)**0.6
    tre_cal = tsl + Q_the/(Gsl*Cw)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Iba_cal = Pbar*1000/Vba
    
    x_f_const[0] = zfc*((Ifc - If)/tau_e)
    x_f_const[1] = zfc*((Gff_mol - Gh2)/tau_f)
    x_f_const[2] = zfc*((1/KO2*(Go2 - If*Kr) - pO2)/tau_O2)
    x_f_const[3] = zfc*((1/KH2O*Gh2r - pH2O)/tau_H2O)
    x_f_const[4] = zfc*((1/KH2*(Gh2- Gh2r) - pH2)/tau_H2)
    x_f_const[5] = zma*((kma1*Gfm**2 + kma2*Gfm + kma3 - Pmtf)/tau_mtf) # MA
    x_f_const[6] = zma*((kma4*Pmtf - tabf)/tau_abf)
    x_f_const[7] = zma*((kma5*Gab + kma6 - tabw)/tau_abw)
    x_f_const[8] = zma*((kma7*trec + kma8 - tabt)/tau_abt)
    x_f_const[9] = zec*((Gr*(hcri - hcro) + alfar*Aci*(tcs - tc))/(Ccr*Mcr)) # EC
    x_f_const[10] = zec*((alfar*Aci*(tc - tcs) + alfaw*Aco*(tcwm - tcs))/(Cs*Mcs))
    x_f_const[11] = zec*((Cw*Gcw*(tcwi - tcwo) + alfaw*Aco*(tcs - tcwm))/(Cw*Mcw))
    x_f_const[12] = zec*((Gr*(heri - hero) + alfar*Aei*(tes - te))/(Cer*Mer))
    x_f_const[13] = zec*((alfar*Aei*(te - tes) + alfaw*Aeo*(tewm - tes))/(Cs*Mes))
    x_f_const[14] = zec*((Cw*Gec*(trec - tec) + alfaw*Aeo*(tes - tewm))/(Cw*Mew))
    x_f_const[15] = iba/C1b - vcap/(R1b*C1b)
    x_f_const[16] = (Iba_cal - Iba)/tau_dc
    x_f_const[17] = (tre_cal - tre)/tau_ahu
    
    return x_f_const

# Outputs
def out_ies_s(xs, xf, us, uf, uz, ud):
    y_alg = MX.zeros(Ny_s)
    
    C_soc = xs[0]
    C_sot = xs[1]
    C_stc = xs[2]
    C_sth = xs[3]
    tbr = xs[4]
    
    If = xf[0]
    Gh2 = xf[1]
    pO2 = xf[2]
    pH2O = xf[3]
    pH2 = xf[4]
    Pmtf = xf[5]
    tabf = xf[6]
    tabw = xf[7]
    tabt = xf[8]
    tc = xf[9]
    tcs = xf[10]
    tcwm = xf[11]
    te = xf[12]
    tes = xf[13]
    tewm = xf[14]
    vcap = xf[15]
    Iba = xf[16]
    tre = xf[17]
    
    Gab = us[0]
    Gec = us[1]
    Gstu = us[2]
    
    Gff = uf[0]
    Gfm = uf[1]
    Nec = uf[2]
    Pbar = uf[3]
    
    zfc = uz[0]
    zma = uz[1]
    zec = uz[2]
    zst = uz[3]
    
    ta = ud[0]
    Sra = ud[1]
    Pd = ud[2]
    Qother = ud[3]
    
    # PV
    I_pv_max = I_max_0*Sra/Sra_pv_0*(1 + c_pv1*(ta - T_pv_0))
    V_pv_max = V_max_0*log(exp(1) + c_pv2*(Sra - Sra_pv_0))*(1 - c_pv3*(ta - T_pv_0))
    Ppv = npp*nsp*I_pv_max*V_pv_max/1000
        
    # FC
    eta_a = afc + bfc*log(If + eps)
    eta_c = -R0fc*T0fc/(2*F0)*log(1-If/IL + eps)
    eta_o = If*rfc
    V0 = N0fc*(E0fc + R0fc*T0fc/(2*F0)*log(pH2*sqrt(pO2)/(pH2O + eps) + eps))
    Vfc = V0 - eta_a - eta_c - eta_o
    Ifc = fu_d/(2*Kr)*Gh2
    Pfc = zfc*(Vfc*Ifc/1000)
    
    # PIP in the rerurn side
    Gall = Gab + Gec + Gstu
        
    # MA
    Pmt = zma*(Pmt0 + Pmtf)
    
    # EC
    pc = exp(21.3-2025.5/(248.94+tc))/1e6
    pe = exp(21.3-2025.5/(248.94+te))/1e6
    pr = pc/pe # Compressor
    etavl = 0.98-0.085*(pr**(1/kr)-1)
    etacp = 0.9085*exp(-0.06443*pr)-7.605*exp(-3.155*pr)
    Gr = etavl*Nec*roeg*Vcp
    wi = kr/(1e3*(kr-1))*pe*1e6/roeg*(pr**((kr-1)/kr)-1)
    Pcp = zec*(Gr*wi/etacp)
    
    # BR
    Hpmp = Spmp*Gall**2/(rhow*ge)
    eta_pmp = b0p*Gall**2 + b1p*Gall + b2p
    Ppmp = Gall*ge*Hpmp/(1e3*eta_pmp)
    
    # BA
    iba = Iba/npb
    Vba = nsb*(Em - vcap - R0b*iba)
    Pba = Vba*Iba/1000
    
    # Electric Power
    Psc = Pcp + Ppmp
    Psl = Ppv + Pfc + Pmt + Pba - Psc - Pd
    
    y_alg[0] = Psl
    y_alg[1] = tbr
    
    return y_alg

# In[2] Formulate discrete time dynamics {xs, xf, us, uf, uz, ud}
x_sym_s = SX.sym('x_s', Nx_s)
x_f_sym_s = SX.sym('x_f_s', Nx_f)

u_sym_s = SX.sym('u_s', Nuc_s)
u_f_sym_s = SX.sym('u_f_s', Nuc_f)

uz_sym_s = SX.sym('uz_s', Nuz_h)
ud_sym_s = SX.sym('ud_s', Nud_s)

ode_sym_s = ode_ies_s(x_sym_s, x_f_sym_s, u_sym_s, 
                      u_f_sym_s, uz_sym_s, ud_sym_s)
args_s = {'x': x_sym_s, 'p': vertcat(x_f_sym_s, u_sym_s, u_f_sym_s, 
                                     uz_sym_s, ud_sym_s), 'ode': ode_sym_s}
opts_s = {'tf': Delta_s, 'regularity_check': True}
I_ode_s = integrator('I_ode_s', 'rk', args_s, opts_s)

